package com.ikas.ai.handle;

import cn.hutool.core.lang.ObjectId;
import com.alibaba.fastjson.serializer.DoubleSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import com.ikas.ai.utils.DateTimeUtil;

import java.text.DecimalFormat;
import java.util.Date;

/**
 * JSON序列，针对fastjson
 *
 * @author HXH
 * @version 1.0.0
 * @ClassName: JsonSerialize
 * @date 2016年12月15日下午6:15:14
 */
public class JsonSerialize {

    private static final SerializeConfig globalInstance = SerializeConfig.getGlobalInstance();
    private static final SerializeConfig ALL = new SerializeConfig();

    public static final ObjectSerializer DOUBLE = new DoubleSerializer(new DecimalFormat("#.##"));
    public static final ObjectSerializer DATE = new SimpleDateFormatSerializer(DateTimeUtil.getDateTimePattern());
    public static final ObjectSerializer OBJECT_ID = (s, o, fn, ft, f) -> s.out.writeString(o == null ? "null" : o.toString());

    static {
        // all config
        ALL.put(Double.class, DOUBLE);
        ALL.put(Date.class, DATE);
        ALL.put(ObjectId.class, OBJECT_ID);
        // 配置需要序列的指定类
        globalInstance.put(Date.class, DATE);
        globalInstance.put(ObjectId.class, OBJECT_ID);
    }

    public static SerializeConfig instance() {
        return globalInstance;
    }

    public static SerializeConfig all() {
        return ALL;
    }
}